var api = require("../../api.js"), 
  app = getApp(),
  images = require('../../utils/images.js');
Page({
    data: {
        contact_tel: "",
        show_customer_service: 0,
        integral: 160,
        userLevelIcon: images.user.userLevelIcon,
        pointsIcon: images.user.pointsIcon,
        balanceIcon: images.user.balanceIcon,
        list: [
          {
            'title': '业务员',
            'icon': images.user.salesmanIcon,
            'path': '',
            'open-type': 'navigator'
          },
          {
            'title': '微客',
            'icon': images.user.distributorIcon,
            'open-type': 'navigator',
            'path': "/pages/share/index"
          },
          {
            'title': '秒杀',
            'icon': images.user.seckillIcon,
            'open-type': 'navigator',
            'path': '/pages/miaosha/order/order'
          },
          {
            'title': '拼团',
            'icon': images.user.groupBookingIcon,
            'open-type': 'navigator',
            'path': '/pages/pt/order/order'
          },
          {
            'title': '砍价',
            'icon': images.user.bargainIcon,
            'open-type': 'navigator',
            'path': '/pages/cut/order/order'
          },
          {
            'title': '收藏',
            'icon': images.user.collectIcon,
            'open-type': 'navigator',
            'path': '/pages/favorite/favorite'
          },
          {
            'title': '优惠券',
            'icon': images.user.couponIcon,
            'open-type': 'navigator',
            'path': '/pages/coupon/coupon'
          },
          {
            'title': '卡券',
            'icon': images.user.cardIcon,
            'open-type': 'navigator',
            'path': '/pages/card/card'
          },
          {
            'title': '在线客服',
            'icon': images.user.serviceIcon,
            'open-type': 'navigator',
            'path': '/pages/info/info'
          },
          {
            'title': '关于我们',
            'icon': images.user.aboutUsIcon,
            'open-type': 'navigator',
            'path': '/pages/article-detail/article-detail'
          },
        ]
    },
    onLoad: function(e) {
        app.pageOnLoad(this);
        this.updateData()
    },
    updateData() {
      const userInfo = wx.getStorageSync("user_info");
      if (userInfo.level === -1) {
        const list = this.data.list
        // list[1].title = '成为微客'
        list[1].path = '/pages/add-share/index'
        this.setData({
          list
        })
      }
    },
    loadData: function(e) {
        var a = this;
        a.setData({
            store: wx.getStorageSync("store")
        });
        var t = wx.getStorageSync("pages_user_user");
        t && a.setData(t), app.request({
            url: api.user.index,
            success: function(e) {
                0 == e.code && (a.setData(e.data), wx.setStorageSync("pages_user_user", e.data), 
                wx.setStorageSync("share_setting", e.data.share_setting), wx.setStorageSync("user_info", e.data.user_info),
                a.updateData(), 
                console.log(e.data));
            }
        });
    },
    onReady: function() {},
    onShow: function() {
        app.pageOnShow(this);
        this.loadData();
    },
    callTel: function(e) {
        var a = e.currentTarget.dataset.tel;
        wx.makePhoneCall({
            phoneNumber: a
        });
    },
    apply: function(a) {
        var t = wx.getStorageSync("share_setting"), o = wx.getStorageSync("user_info");
        console.log(a), 1 == t.share_condition ? wx.navigateTo({
            url: "/pages/add-share/index"
        }) : 0 != t.share_condition && 2 != t.share_condition || (0 == o.is_distributor ? wx.showModal({
            title: "申请成为分销商",
            content: "是否申请？",
            success: function(e) {
                e.confirm && (wx.showLoading({
                    title: "正在加载",
                    mask: !0
                }), app.request({
                    url: api.share.join,
                    method: "POST",
                    data: {
                        form_id: a.detail.formId
                    },
                    success: function(e) {
                        0 == e.code && (0 == t.share_condition ? (o.is_distributor = 2, wx.navigateTo({
                            url: "/pages/add-share/index"
                        })) : (o.is_distributor = 1, wx.navigateTo({
                            url: "/pages/share/index"
                        })), wx.setStorageSync("user_info", o));
                    },
                    complete: function() {
                        wx.hideLoading();
                    }
                }));
            }
        }) : wx.navigateTo({
            url: "/pages/add-share/index"
        }));
    },
    verify: function(e) {
        wx.scanCode({
            onlyFromCamera: !1,
            success: function(e) {
                console.log(e), wx.navigateTo({
                    url: "/" + e.path
                });
            },
            fail: function(e) {
                wx.showToast({
                    title: "失败"
                });
            }
        });
    },
    member: function() {
        wx.navigateTo({
            url: "/pages/member/member"
        });
    },
    onClickItem(event) {
      const index = event.currentTarget.dataset.index
      const item = this.data.list[index]
      if (item['open-type'] === 'navigator') {
        wx.navigateTo({
          url: item.path
        })
      } else if (index === 1) {
        this.applyDistributor()
      }
    },
    applyDistributor() {
      const that = this
      const t = wx.getStorageSync("share_setting"), o = wx.getStorageSync("user_info");

      wx.showModal({
        title: "申请成为分销商",
        content: "是否申请？",
        success: function(e) {
            e.confirm && (wx.showLoading({
                title: "正在加载",
                mask: !0
            }), app.request({
                url: api.share.join,
                method: "POST",
                data: {
                    form_id: o.detail.formId
                },
                success: function(e) {
                    0 == e.code && (0 == t.share_condition ? (o.is_distributor = 2, wx.navigateTo({
                        url: "/pages/add-share/index"
                    })) : (o.is_distributor = 1, wx.navigateTo({
                        url: "/pages/share/index"
                    })), wx.setStorageSync("user_info", o));
                },
                complete: function() {
                    wx.hideLoading();
                }
            }));
        }
       }) 
    }
})